package com.rose.doorto.footbathdoor.controler;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rose.doorto.footbathdoor.pojo.Project;
import com.rose.doorto.footbathdoor.pojo.Teacher;
import com.rose.doorto.footbathdoor.service.IProjectService;
import com.rose.doorto.footbathdoor.service.TeacherService;
import com.rose.doorto.footbathdoor.util.Result;
import com.rose.doorto.footbathdoor.vo.FormVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lqc
 * @since 2024-07-22
 */
//@RestController
@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private IProjectService service;



    /**
     * 注册技师信息
     * @param teacher
     * @return
     */
    @PostMapping("/addITeacher")
    public Result addITeacher(Teacher teacher){
        int i = teacherService.addITeacher(teacher);
        if (i>0){
            return Result.success(i,200,"注册成功");
        }else {
            return Result.error(500,"注册失败");
        }
    }

    @PostMapping("/addITeacher2")
    public Result addITeacher2(Teacher teacher){
        boolean save = teacherService.save(teacher);
        if (save){
            return Result.success(200,200,"注册成功");
        }else {
            return Result.error(500,"注册失败");
        }
    }

    /**
     * 技师信息修改
     * @param teacher
     * @return
     */
    @PostMapping("updateITeacher")
    public Result updateITeacher(Teacher teacher){
        int i = teacherService.updateITeacher(teacher);
        if (i>0){
            return Result.success(i,200,"修改成功");
        }else {
            return Result.error(500,"修改失败");
        }
    }

    @PostMapping("updateITeacher2")
    public Result updateITeacher2(Teacher teacher){
        boolean update = teacherService.update(teacher, null);
        if (update){
            return Result.success(666,200,"修改成功");
        }else {
            return Result.error(500,"修改失败");
        }
    }

    /**
     * 按照人气值进行排序
     * @return
     */
    @GetMapping("/orderByScore")
    public Result orderByScore() {
        List<Teacher> teacher = teacherService.orderByScore();
        if (teacher!=null){
            return Result.success(teacher,200,"排序完成");
        }else {
            return Result.error(500,"排序失败");
        }
    }

    @GetMapping("/orderByScore2")
    public Result orderByScore2() {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        List<Teacher> score = teacherService.list(wrapper.orderBy(true, false, "score"));
        if (score!=null){
            return Result.success(score,200,"排序完成");
        }else {
            return Result.error(500,"排序失败");
        }
    }

    /**
     * 按照距离进行排序
     * @return
     */
    @GetMapping("/orderByDistance")
    public Result orderByDistance(){
        List<Teacher> distance = teacherService.orderByDistance();
        if (distance!=null){
            return Result.success(distance,200,"排序完成");
        }else {
            return Result.error(500,"排序失败");
        }
    }

    @GetMapping("/orderByDistance2")
    public Result orderByDistance2(){
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        List<Teacher> distance = teacherService.list(wrapper.orderBy(true, true, "distance"));
        if (distance!=null){
            return Result.success(distance,200,"排序完成");
        }else {
            return Result.error(500,"排序失败");
        }
    }

    /**
     * 获取技师详情信息并展示
     * @param tid
     * @param model
     * @return
     */
    @GetMapping("/getTeacherById/{tid}")
    public String getTeacherById(@PathVariable("tid") Integer tid, Model model){
//        List<Teacher> teacher = teacherService.getTeacherByProjectId(id);
        FormVo formVo = new FormVo();
        Teacher teacher = teacherService.getById(tid);
        formVo.setTid(teacher.getTid());   //技师编号
        formVo.setName(teacher.getName()); //技师名字
        formVo.setImage(teacher.getImage()); //技师头像
        formVo.setGender(teacher.getGender()); //性别
        formVo.setNation(teacher.getNation()); //民族
        formVo.setAge(teacher.getAge());       //年纪
        formVo.setHealth(teacher.getHealth()); //身高
        formVo.setWeight(teacher.getWeight()); //体重
        formVo.setIntroduce(teacher.getIntroduce()); //个人介绍
        formVo.setPhoto(teacher.getPhoto());   //个人相册
        formVo.setDistance(teacher.getDistance());  //距离

//        model.addAttribute("teachers",teacher);
        List<Project> list = service.getProjectListById(tid);
        for (Project project : list) {
            formVo.setPimage(project.getImage());  //项目图片
            formVo.setIntroduction(project.getIntroduction());  //项目简介
            formVo.setPrice(project.getPrice());   //项目价格
            formVo.setSell(project.getSell());     //已售数量
            formVo.setPid(project.getPid());        //项目编号
            formVo.setDuration(project.getDuration());  //服务时长
        }

//        model.addAttribute("lists",list);
        model.addAttribute("formVo",formVo);
        return "/showDetail.html";
    }


    /**
     * 条件分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/list")
    public String getEntityList(@RequestParam(defaultValue = "1") int pageNum,
                                        @RequestParam(defaultValue = "10") int pageSize,
                                        @RequestParam(required = false) String name,
                                        @RequestParam(required = false) int id,
                                        Model model) {
        IPage<Teacher> pageData = teacherService.getEntitiesByConditions(pageNum, pageSize, name,id);
        model.addAttribute("pageData",pageData);
        return "/teacherlist.html";
    }



}
